import { nextTick, ref } from "vue";
import { dragBtnStyleRule, tabListModuleRule } from "./index";
import { objectAny } from "../../options/base";
import { timeOut } from "../../utils/utils";


export function buildFullBottomPopup(){
    // 联系弹出模块
    const riderConnectRef = ref<any>(null)
    // 转单弹出模块
    const riderTurnOrderRef = ref<any>(null);
    // 用户操作 转单 联系模块时 对应的数据包
    const riderCurrentPopupItData = ref<objectAny>({})
    // 用户操作 转单 联系模块时 对应的底部按钮样式集合
    const riderCurrentPopupItDragBtnStyle = ref<dragBtnStyleRule | {}>({})
    // 转单二维码
    const turnOrderQrcodeRef = ref<any>(null)


    const refStatusToMap= {
        0:{
            refValue:riderConnectRef,
            callFunList:[]
        },
        1:{
            refValue:riderTurnOrderRef,
            callFunList:[turnOrderCreateQrcode] as Array<Function>
        }
    }
    // 转单弹出层显示时 构建二维码显示
    async function  turnOrderCreateQrcode(){
        await timeOut(200)
        console.log(`turnOrderCreateQrcode 创建二维码方法`)
        if(turnOrderQrcodeRef.value){
            turnOrderQrcodeRef.value.make({
                success:()=>{
                    console.log(`二维码显示成功`)
                },
                fail:()=>{
                    console.error(`二维码显示失败`)
                }
            })
        }else{
            console.warn(`页面未构建完成，无法创建转单二维码`)
        }
    }
    
    // 开启弹出层
    async function openPopup(
        serverData:objectAny,
        dragBtnStyle:dragBtnStyleRule,
        status:0|1
    ){
        riderCurrentPopupItData.value=serverData;
        riderCurrentPopupItDragBtnStyle.value= dragBtnStyle;
        
        await nextTick();
        const {refValue,callFunList} = refStatusToMap[status]
        refValue.value.open()
        await nextTick();
        callFunList[0]&&callFunList[0]()
    }
    // 关闭弹出层
    async function closePopup(status:0|1){
        const {refValue,callFunList} = refStatusToMap[status]
        refValue.value.close()
    }

    return {
        turnOrderQrcodeRef,
        riderConnectRef,
        riderTurnOrderRef,
        riderCurrentPopupItData,
        riderCurrentPopupItDragBtnStyle,

        turnOrderCreateQrcode,
        openPopup,
        closePopup,
    }
}
